import 'package:base/main.dart';
import 'package:framework/core/base_view_model.dart';
import 'package:framework/core/type_registry.dart';
import 'package:get/get.dart';

abstract class BaseLogic extends GetxController {
  final _isLoading = false.obs;

  bool get isLoading => _isLoading.value;

  void setLoading(bool value) => _isLoading.value = value;

  void ready();

  final List<VMModel> _vms = [];

  T register<T extends BaseViewModel>(T vm,
      {bool force = false, String tag = ""}) {
    final type = T;
    if (!Get.isRegistered<T>(tag: tag)) {
      Get.put<T>(vm, permanent: force, tag: tag);
      TypeRegistry.registerDeleter<T>();
      _vms.add(VMModel(type, vm, force, tag));
    }
    return vm;
  }

  @override
  void onClose() {
    for (var vm in _vms) {
      TypeRegistry.delete(vm.type);
    }
    _vms.clear();
    super.onClose();
  }
}

class VMModel {
  final Type type;
  final BaseViewModel model;
  final bool force;
  final String tag;

  VMModel(this.type, this.model, this.force, this.tag) {}
}
